package book.book.service;

import book.book.bean.BookType;
import book.book.bean.Books;
import book.book.bean.Borrowinfo;
import book.book.dao.*;
import book.book.util.Base;

import java.awt.print.Book;
import java.util.List;

public class BooksServiceImpl extends Base implements BooksService {
    BooksDao booksDao = new BooksDaoImpl();
    BookTypeDao bookTypeDao = new BookTypeDaoImpl();
    BorrowinfoDao borrowinfoDao = new BorrowinfoDaoImpl();

    @Override
    public boolean addBook(Books books) {
        String bookname = books.getBookname();
        String author = books.getAuthor();
        Books book = booksDao.queryBookByName(bookname, author);
        if (book == null) {
            booksDao.insertBook(books);
            return true;
        }
        return false;
    }

    @Override
    public boolean delBookById(String bookname, String author) {
        Books books = booksDao.queryBookByName(bookname, author);
        if( books == null){
            System.out.println("不存在该书！");
            return false;
        }
        String id = books.getBookid();
       List<Borrowinfo> borrowinfo = borrowinfoDao.selectBorrwInfoByBookId(id);
        if (borrowinfo.size()!= 0){
            System.out.println("该书有借阅信息，不能删除！");
            return false;
        }
        return booksDao.deleteBook(id) > 0;
    }

    @Override
    public boolean updateBookInfo(Books books) {
        String booktype = books.getBooktype();
        BookType bookType = bookTypeDao.getBookType(booktype);
        if(booktype == null){
            System.out.println("不存在该类型！");

            return false;
        }
        books.setBooktype(bookType.getTid());
        int i =  booksDao.updateBook(books);
        if(i > 0){
            System.out.println("修改成功！");
            return true;
        }

        return false;

    }

    @Override
    public List<Books> queryBookLikeName(String name) {
        List<Books> books = booksDao.selectBookLikeName(name);
        return books;

    }

    @Override
    public List<Books> selectAllBooks() {
        List<Books> books = booksDao.selectallBooks();
        return books;
    }

    @Override
    public Books selectBooksByname(String name, String author) {
        Books books = booksDao.queryBookByName(name, author);
        return books;
    }
}

